#include "mapeditorpanel.h"
#include "evemapeditor.h"

BEGIN_EVENT_TABLE(MapEditorPanel, wxScrolledWindow)
   EVT_IDLE(MapEditorPanel::OnIdle)
   EVT_LEFT_DOWN(MapEditorPanel::OnLButton)
   EVT_LEFT_UP(MapEditorPanel::OnLButton)
   EVT_MOTION(MapEditorPanel::OnMouseMove) 
   EVT_KEY_DOWN(MapEditorPanel::OnKey)
   //EVT_MAXIMIZE(MapEditorPanel::OnMaxSize)
END_EVENT_TABLE()

MapEditorPanel::MapEditorPanel(wxWindow *parent, int id,int x, int y, int w, int h)
: wxScrolledWindow( parent, id, wxPoint(x, y), wxSize(w, h))
, m_pMapEditor(NULL)
{
	m_pMapEditor = new MapEditor();
	m_pMapEditor->InitWithhWnd(static_cast<HWND>(GetHWND()));


	SetScrollbars(10,10,50,50);
	SetVirtualSize(2000,2000);
	this->EnableScrolling(true,true);
}

MapEditorPanel::~MapEditorPanel()
{
	if( m_pMapEditor )
	{
		m_pMapEditor->Release();
		SAFE_DELETE(m_pMapEditor);
	}
}

void MapEditorPanel::OnIdle( wxIdleEvent& event )
{
	if( m_pMapEditor )
	{
		m_pMapEditor->Run();
	}
//	event.skip();
}

void MapEditorPanel::OnLButton(wxMouseEvent& event)
{
	if( m_pMapEditor )
	{

		int x = event.GetX();
		int y = event.GetY();
		
		const wxSize rc = GetTargetSize();
		x *= GetSize().x/Real(rc.x);
		y *= GetSize().y/Real(rc.y);
		ulong pos = ( x | (y << 16) );
		if( event.ButtonDown() )
			Inputer::GetSingleton()->ProcessMouseInputMsg(eve::EVE_BUTTONDOWN,eve::LeftButton,0,pos);
		else
			Inputer::GetSingleton()->ProcessMouseInputMsg(eve::EVE_BUTTONUP,eve::LeftButton,0,pos);
	}
	event.Skip();
}
void MapEditorPanel::OnMouseMove(wxMouseEvent& event)
{
	if( m_pMapEditor )
	{
		//wxCoord
		int x = event.GetX();
		int y = event.GetY();
		//wxSize rc = GetTargetSize();
		//x *= GetSize().x/Real(rc.x);
		//y *= GetSize().y/Real(rc.y);
		ulong pos = ( x | (y << 16) );
		Inputer::GetSingleton()->ProcessMouseInputMsg(eve::EVE_MOUSEMOVE,eve::LeftButton,0,pos);
	}
	event.Skip();
}

void MapEditorPanel::OnKey(wxKeyEvent& event)
{
	if( m_pMapEditor )
	{
		int code = event.GetKeyCode();
		Inputer::GetSingleton()->ProcessKeyInputMsg(eve::EVE_CHAR,code,0);
	}
	event.Skip();
}




WXLRESULT MapEditorPanel::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
{
	switch( nMsg )
	{
	case WM_WINDOWPOSCHANGED:
		{
			if( m_pMapEditor )m_pMapEditor->Reset();
		}
		return 1;
	}
	return wxScrolledWindow::MSWDefWindowProc(nMsg, wParam, lParam);
}